package com.example.demo.model.admin.admin.controler;

import com.example.demo.Results.ResultClass;
import com.example.demo.config.RandomValidateCodeUtil;

import com.example.demo.model.admin.admin.entity.eid.UpdatePasswd;
import com.example.demo.model.admin.admin.entity.reg.LoginReq;
import com.example.demo.model.admin.admin.service.ShopAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author CHOKSTAHYW
 * @date 2021/9/15 - 14:44
 */
@Api(tags = "登录注册")
@Slf4j
@RestController
@RequestMapping("/admin/shopAdmin")
public class ShopAdminResource {

    @Autowired
    ShopAdminService shopAdminService;



    /**
     * 退出方法
     * @return
     */
    @ApiOperation(value = "登录方法", notes = "登录方法")
    @PostMapping("/doLogout")
    public ResultClass doLogout(HttpServletRequest request){
        request.getSession().removeAttribute("adminId");
        request.getSession().removeAttribute("adminName");
        return ResultClass.ok();
    }

    /**
     * 退出方法
     * @return
     */
    @ApiOperation(value = "退出方法", notes = "退出方法")
    @GetMapping("/doLogout")
    public ModelAndView GetdoLogout(HttpSession session){

        session.invalidate();
        ModelAndView mv=new ModelAndView();
        return mv;
    }




    /**
     * 修改密码方法
     * @return
     */
    @ApiOperation(value = "修改密码方法", notes = "修改密码方法")
    @PostMapping("/editPasswd")
    public ResultClass editPasswd(HttpServletRequest request, UpdatePasswd updatePasswd){
        return  shopAdminService.editPasswd(request,updatePasswd);
    }


    /**
     * 登录方法
     * @return
     */
    @ApiOperation(value = "登录方法", notes = "登录方法")
    @PostMapping("/doLogin")
    public ResultClass doLogin(HttpServletRequest request, LoginReq loginReq){
        return shopAdminService.doLogin(request,loginReq);
    }

    /**
     * 注册方法
     * @return
     */
    @ApiOperation(value = "注册方法", notes = "注册方法")
    @PostMapping("/doReg")
    public ResultClass doReg(HttpServletRequest request, LoginReq loginReq){
        return shopAdminService.doReg(request,loginReq);
    }

    /**
     * 修改密码页
     * @return
     */
    @ApiOperation(value = "修改密码页", notes = "修改密码页")
    @GetMapping("/editPasswd")
    public ModelAndView editPasswd(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/admin/editPasswd");
        return  mv;
    }

    /**
     * 登录页
     * @return
     */
    @ApiOperation(value = "登录页", notes = "登录页")
    @GetMapping("/login")
    public ModelAndView login(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/admin/login");
        return  mv;
    }

    @ApiOperation(value = "注册页", notes = "注册页")
    @GetMapping("/reg")
    public ModelAndView reg(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/admin/reg");
        return  mv;
    }
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping("/getValidateCode")
    public void  getValidateCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置相应类型,告诉浏览器输出的内容为图片
            response.setContentType("image/jpeg");
            //设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandomCode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            log.error("获取验证码失败>>>>   ", e);
        }
    }


}

